/* 
1 输入框绑定 值改变事件 input事件
  1 获取到输入框的值
  2 合法性判断 
  3 检验通过 把输入框的值 发送到后台
  4 返回的数据打印到页面上
2 防抖 （防止抖动） 定时器  节流 
  0 防抖 一般 输入框中 防止重复输入 重复发送请求
  1 节流 一般是用在页面下拉和上拉 
  1 定义全局的定时器id
 */
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  data: {
    TabInfos:[],
    // 取消 按钮 是否显示
    isFocus:false,
    // tags
    tags:[]
  },

  inpValue:"",

  onLoad: function (options) {
    // if from category, immediate search request in need
    if(options.from_category){
      // set tags
      let eventChannel = this.getOpenerEventChannel();
      var that = this
      eventChannel.on('tags', function(data){
        that.setData({
          tags: data.tags
        })
      })
      // perform search request
      this.search();
    }
  },

  async search() {
    const user_info = wx.getStorageSync('user_info');
    const data = {
      mode: 1,
      user_id: user_info ? user_info.id : 0,
      tags: this.data.tags.map(element => element.id),
      str: this.inpValue
    };
    // const res = await request({
    //   url: "/tabinfos",
    //   data: data
    // });
    var TabInfos=[
      {
        id: 0,
        goods_name: "Test Case",
        goods_price: 23.33,
        tags: [
          {
              id: 0,
              tag_name: "tag0",
              image_src: "/static/1.jpg"
          },
          {
              id: 2,
              tag_name: "tag2",
              image_src: "/static/2.jpeg"
          },
          {
              id: 85,
              tag_name: "tag85",
              image_src: "/static/3.jpeg"
          },
          {
              id: 166,
              tag_name: "tag166",
              image_src: "/static/c1.png"
          }
        ],
        image_src:"/static/g1.jpg",
        marked:false
      },
      {
        id: 2333,
        goods_name: "Test Case 2",
        goods_price: 2000,
        tags: [
          {
              id: 0,
              tag_name: "tag0",
              image_src: "/static/1.jpg"
          },
          {
              id: 2,
              tag_name: "tag2",
              image_src: "/static/2.jpeg"
          },
          {
              id: 85,
              tag_name: "tag85",
              image_src: "/static/3.jpeg"
          }
        ],
        image_src: "/static/g2.jpg",
        marked:true
      }
    ]
    this.setData({
      TabInfos:TabInfos
    })
  },
  
  // tigger when input
  handleInput(e){
    const {value}=e.detail;
    // record input value
    this.inpValue = value;
    // set button status
    if(!value.trim() && !this.tids.length){
      this.setData({
        isFocus:false,
      })
    }
    else{
      this.setData({
        isFocus:true,
      })
    }
  },

  // tigger when pushing 'return'
  handleConfirm(e){
    // No need to perform value check since it's done in 'handleInput'
    this.search();
  },

  // tap 'search' button
  handleSearch(e){
    // No need to perform value check since it's done in 'handleInput'
    this.search();
  },

  handleTags(e){
    var that = this;
    wx.navigateTo({
      url: "/pages/tags/tags",
      events: {
        tags: function(data) {
          that.setData({
            tags: data.tags
          })
        }
      },
      success: function(res) {
        res.eventChannel.emit("tags", {tags: that.data.tags});
      }
    })
  },

  handleFilter(e){
    wx.navigateTo({
      url: '/pages/filter/filter',
    })
  }
})
